package com.umeox.watch.moto.launcher.ui.widget;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.AnimationDrawable;
import android.os.BatteryManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeox.watch.moto.launcher.R;

import java.util.Locale;


public class HomeBatteryStatusView extends RelativeLayout {

    public static final int BATTERY_STATUS_CHARGING_FULL = 9999;
    private TextView mBatteryTv;
    private ImageView mBatteryIv;
    private Context mContext;
    private AnimationDrawable mAnimationDrawable;

    public HomeBatteryStatusView(Context context) {
        this(context, null);
    }

    public HomeBatteryStatusView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HomeBatteryStatusView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();

    }

    private void initView() {
        final View view = LayoutInflater.from(getContext()).inflate(R.layout.view_battery_home, this);
        mBatteryIv = view.findViewById(R.id.iv_battery);
        mBatteryTv = view.findViewById(R.id.tv_battery);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        final IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        mContext.registerReceiver(mBatteryReceiver, intentFilter);

    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mContext.unregisterReceiver(mBatteryReceiver);
    }

    /**
     * 设置电池电量
     *
     * @param level
     */
    private void setupBatteryLevel(int level) {
        releaseAnimationDrawable();
        mBatteryIv.setImageResource(R.drawable.battery_level_home);
        mBatteryIv.setImageLevel(level);
    }

    /**
     * 释放电池充电动画
     */
    private void releaseAnimationDrawable() {
        if (mAnimationDrawable != null) {
            mAnimationDrawable.stop();
            mBatteryIv.setImageDrawable(null);
        }
    }

    private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            mBatteryTv.setText(String.format(Locale.getDefault(), "%d%%", level));

            switch (status) {
                case BatteryManager.BATTERY_STATUS_CHARGING:
                    if(level == 100){
                        setupBatteryLevel(BATTERY_STATUS_CHARGING_FULL);
                    }else{
                        mBatteryIv.setImageResource(R.drawable.battery_anim_home);
                        mAnimationDrawable = (AnimationDrawable) mBatteryIv.getDrawable();
                        mAnimationDrawable.start();
                    }
                    break;
                case BatteryManager.BATTERY_STATUS_DISCHARGING:
                    setupBatteryLevel(level);
                    break;
                case BatteryManager.BATTERY_STATUS_FULL:
                    setupBatteryLevel(BATTERY_STATUS_CHARGING_FULL);
                    break;
            }
        }
    };

}
